/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TerminerSejour.java
 *
 * Created on Jun 29, 2012, 9:31:58 PM
 */
package View;

import Controller.PaiementController;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.WindowConstants;

/**
 *
 * @author fokasu
 */
public class TerminerSejour extends AbstractViewPanel {
    PaiementController controller;
    
    /** Creates new form TerminerSejour */
    public TerminerSejour(PaiementController paiementController) {
        this.controller = paiementController;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNumChambre = new javax.swing.JLabel();
        txtNumChambre = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblChambre = new javax.swing.JTable();
        String[] header = new String[2];
        header[0]  = "Num. Chambre";
        header[1] = "Catégorie";
        JTable jtbl = new JTable(new String[0][0], header);
        tblChambre.setModel(jtbl.getModel());
        btnAjouter = new javax.swing.JButton();
        btnTerminer = new javax.swing.JButton();

        lblNumChambre.setText("Numéro de chambre");

        tblChambre.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Num. Chambre", "Catégorie"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblChambre);

        btnAjouter.setText("Ajouter");
        btnAjouter.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAjouterMouseClicked(evt);
            }
        });

        btnTerminer.setText("Terminer");
        btnTerminer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnTerminerMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(lblNumChambre)
                    .addComponent(btnTerminer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNumChambre, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAjouter, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(lblNumChambre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumChambre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAjouter))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnTerminer)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAjouterMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAjouterMouseClicked
        controller.addChambre(Integer.parseInt(this.txtNumChambre.getText()));
    }//GEN-LAST:event_btnAjouterMouseClicked

    private void btnTerminerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnTerminerMouseClicked
        //afficher la nouvelle Interface
        PayerSejour view = new PayerSejour(controller);

        controller.addView(view);

        view.updateAll();

        JFrame displayFrame = new JFrame("Confirmer la réservation");
        displayFrame.getContentPane().add(view, BorderLayout.CENTER);
        displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        displayFrame.pack();
        displayFrame.setVisible(true);

    }//GEN-LAST:event_btnTerminerMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAjouter;
    private javax.swing.JButton btnTerminer;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNumChambre;
    private javax.swing.JTable tblChambre;
    private javax.swing.JTextField txtNumChambre;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        switch(evt.getPropertyName()){
            case PaiementController.LISTE_CHAMBRE_PROPERTY:
                buildListeChambreTable((String[][]) evt.getNewValue());
                break;
            case PaiementController.AJOUTER_PAIEMENT_EVENT:
                this.getTopLevelAncestor().setVisible(false);
                break;
        }
    }

    private void buildListeChambreTable(String[][] listechambre) {
         String[] celNames;
        
        String[] header = new String[2];
        header[0]  = "Num. Chambre";
        header[1] = "Catégorie";
        
        JTable jtbl = new JTable(listechambre, header);
        this.tblChambre.setModel(jtbl.getModel()); 
    }
}
